#include "fr_aerian_imageconverter_NativePictureConverter.h"
#include <arm_neon.h>

void JNICALL Java_fr_aerian_imageconverter_NativePictureConverter_ConvertBGRtoRGB(
		JNIEnv* env, jobject obj, jbyteArray pSrc, jbyteArray pDst, jint width, jint height, jint neon){

	jbyte *c_src = (*env)->GetByteArrayElements(env, pSrc, 0);
	jbyte *c_dest = (*env)->GetByteArrayElements(env, pDst, 0);
	int i = 0;
	if (neon == 0){
		        // use NEON-optimized routines
		/*for(i = 0 ; i < width * height / 8 ; i++)
			asm(
					"vld4.8 {d0, d1, d2, d3}, [%0]!\n"
					"vswp d0, d2\n"
					"vst4.8 {d0, d1, d2, d3}, [%1]!\n"
					:
					:"r"(c_src),"r"(c_dest)
					:
				);
		c_src+=4;
		c_dest+=4;*/
		int size = (width * height)/8;
		asm (
				"mov ecx, #0\t\n"
				"loop:\t\n"
					"cmp ecx, [%2]\t\n"
					"ja endLoop\t\n"
					"vld4.8 {d0, d1, d2, d3}, [%0]!\t\n"
					"vswp d0, d2\t\n"
					"vst4.8 {d0, d1, d2, d3}, [%1]!\t\n"
					"jmp loop\t\n"
					"inc ecx\t\n"
				"endLoop:\t\n"
				:"+r"(c_src),"+r"(c_dest),"+r"(size)
				:
				:"d0","d1","d2","d3"
			);
	} else {
		for(i = 0 ; i < width * height * 4 ; i+=4){
			*(c_dest+i) = *(c_src+i+2); 	//Swap  Blue with Red
			*(c_dest+i+2) = *(c_src+i); 	//Swap Red with Blue
		}
	}
}
